﻿using System;
using System.Web.Configuration;
using Ext.Net;
using BG.Data;
using BG.Service;

namespace BG.Web.Admin.Configuration
{
    public partial class Default : BG.Web.Admin.UIPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!X.IsAjaxRequest)
            {
                Page.Title = WebConfigurationManager.AppSettings["title"];
                TConfig t = ConfigManager.Instance.LoadFromCache();
                if (t == null) return;
                txtAddHourTime.SetValue(t.AddHourTime);
                txtAddDayTime.SetValue(t.AddDayTime);
                txtAddHalfTime.SetValue(t.AddHalfTime);
                txtAllowTime.Text = t.AllowTime.ToString();
                txtAutoReadCard.Checked = t.AutoReadCard;
                txtDefaultHours.SetValue(t.DefaultHours);
                txtDeposit.Text = t.Deposit.ToString();
                txtNightTime.SetValue(t.NightTime);
                txtNewDayTime.SetValue(t.NewDayTime);
                txtOutTimePrice.Text = t.OutTimePrice.ToString();
                txtPort.SetValue(t.Port);
            }

        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            TConfig t = (TConfig.entityID==1).ListFirst<TConfig>();
            t.AddDayTime = txtAddDayTime.Text;
            t.AddHalfTime = txtAddHalfTime.Text;
            t.AddHourTime = txtAddHourTime.Text;
            t.AllowTime =  Convert.ToInt32(txtAllowTime.Text);
            t.AutoReadCard = txtAutoReadCard.Checked;
            t.DefaultHours =  Convert.ToInt32(txtDefaultHours.Text);
            t.Deposit = Convert.ToInt32(txtDeposit.Text);
            t.NightTime = txtNightTime.Text;
            t.NewDayTime = txtNewDayTime.Text;
            t.Port = Convert.ToInt32(txtPort.Text);
            t.OutTimePrice = Convert.ToInt32(txtOutTimePrice.Text);
            ConfigManager.Instance.SaveConfig(t);
        }
    }
}